package com.eqdd.yiqidian.base;

import android.content.Intent;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.eqdd.yiqidian.utils.ActivityInterceptor;
import com.trello.rxlifecycle.components.support.RxFragment;


/**
 * Created by vzhihao on 2016/11/2.
 */
public abstract class BaseFragment extends RxFragment implements View.OnClickListener {
    private ViewDataBinding fragmentCustom;
    private boolean isVisable;


    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (fragmentCustom == null) {
            fragmentCustom = initBinding(inflater);
        }
        fragmentCustom.getRoot().setClickable(true);
        ViewGroup parent = (ViewGroup) fragmentCustom.getRoot().getParent();
        if (parent != null) {
            parent.removeView(fragmentCustom.getRoot());
        }
//        isInit = true;
//        /**初始化的时候去加载数据**/
//        isCanLoadData();
        initData();
        setView();

        return fragmentCustom.getRoot();
    }

    //是否可见
    protected boolean isVisble;
    // 标志位，标志Fragment已经初始化完成。
    public boolean isPrepared = false;

    /**
     * 实现Fragment数据的缓加载
     *
     * @param isVisibleToUser
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            isVisable = true;
            onVisible();
        } else {
            isVisable = false;
            onInVisible();
        }
    }

    protected void onInVisible() {
    }

    protected void onVisible() {
        //加载数据
        loadData();
    }

    public void loadData() {

    }

    protected abstract void setView();

    protected abstract void initData();

    public abstract ViewDataBinding initBinding(LayoutInflater inflater);

    /**
     * [页面跳转]
     *
     * @param clz
     */
    public void startActivity(Class<?> clz) {
//        ActivityInterceptor.interceptor((BaseActivity) getActivity(),clz,null,(bun) -> {
        startActivity(clz, null);
//        });
    }

    /**
     * [携带数据的页面跳转]
     *
     * @param clz
     * @param bundle
     */
    public void startActivity(Class<?> clz, Bundle bundle) {
        ActivityInterceptor.interceptor((BaseActivity) getActivity(), clz, bundle, (bun) -> {
            Intent intent = new Intent();
            intent.setClass(getActivity(), clz);
            if (bundle != null) {
                intent.putExtras(bundle);
            }
            startActivity(intent);
        });
    }


}
